<?php

namespace app\api\model;

use think\Model;

class ImGroupUser extends Model
{
    //开启自动完成时间戳功能
    protected $autoWriteTimestamp = true;
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';


    /**
     * Notes:加入群成员
     * User: tangfen
     * Date: 2023/12/21
     * Time: 16:14
     */
    public function AddGroupUser($groupInfo, $membersArray)
    {
        $info = [];
        if (!empty($membersArray)) {
            foreach ($membersArray as $k => $v) {
                $info['uid'] = $v['Member_Account'];
                $info['GroupId'] = $groupInfo['group_id'];
                $info['create_time'] = time();
                $info['update_time'] = time();

                $data = $this->where(['uid' => $v['Member_Account'], 'GroupId' => $groupInfo['group_id']])->value('id');

                if (empty($data)) {
                    $this->insert($info);
                } else {
                    $this->where(['uid' => $v['Member_Account'], 'GroupId' => $groupInfo['group_id']])->update(['status' => 1, 'update_time' => time()]);
                }
            }
        }

        return true;
    }

    /**
     * Notes:退出群
     * User: tangfen
     * Date: 2023/12/21
     * Time: 20:05
     * @param $params
     */
    public function ExitGroup($params)
    {
        $membersArray = explode(',', $params['members']);
        if (!empty($membersArray)) {
            foreach ($membersArray as $k => $v) {
                $this->where(['uid' => $v, 'GroupId' => $params['group_id']])->update(['status' => 2, 'update_time' => time()]);
            }
        }
        return true;
    }



}